package org.entities;

import org.BlockMap;
import org.GameState;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.Animation;

// ==================================
// Generic class for all shoots.
// ==================================


public class Shoot extends LevelObject{

    //private static int size[]={0,0,16,0,16,16,0,16};// bullet dimension
    private String direction;	                    // direction for the bullet 
    public static final float MOVE_SPEED=0.2f;	    // bullet velocity
    private float dx = 0;
    public Image sprite;
    public static boolean bulletExist;

    private Animation bulletLeft,bulletRight, fallback;

    public Shoot(int x,int y,Image image,String direction,String type) throws SlickException {

        super(x, y, 
                0, 0, 
                image.getWidth()/3, 0, 
                image.getWidth()/3, image.getHeight()/2, 
                0, image.getHeight()/2,
                type);

        //sprite = new Image(image);

        // ======================================================
        // create shoot animation. image provided by constructor
        // ======================================================

        SpriteSheet sheet = new SpriteSheet(image,(image.getWidth()/3),image.getHeight()/2);
        bulletLeft = new Animation();
        for (int frame=0;frame<2;frame++) {
            bulletLeft.addFrame(sheet.getSprite(frame,1),90);			
        }
        bulletRight = new Animation();
        for (int frame=0;frame<2;frame++) {
            bulletRight.addFrame(sheet.getSprite(frame,0),90);          
        }
        bulletLeft.setAutoUpdate(false);
        bulletRight.setAutoUpdate(false);
        this.direction=direction;
    }

    public void update(int delta) throws SlickException {

        moveRight=false;
        moveLeft=false;
        dx = (float)delta*2*MOVE_SPEED ;
        float ax = dx;

        if (direction=="left"){
            moveLeft=true;
            fallback=bulletLeft;
            bulletLeft.update(delta);
            x-=ax;
            poly.setX(x);
            poly.setY(y);
            if (GameState.entityCollisionWith(this)){
                destroy();
            }            		
        }

        if (direction=="right"){
            moveRight=true;
            fallback=bulletRight;
            bulletRight.update(delta);
            x+=ax;
            poly.setX(x);   
            poly.setY(y);
            if (GameState.entityCollisionWith(this)){
                destroy();
            }
        }

        // ===============================
        // bullet do not cross map borders
        // ===============================
        if (x < 0){ x = 0;destroy();}
        if (y < 0){ y = 0;destroy();}		
        if (x > BlockMap.mapWidth -48){ x=BlockMap.mapWidth -48;destroy();}
        if (y > BlockMap.mapHeight-48){ y=BlockMap.mapHeight-48;destroy();}
        // }

    }

    // ==============
    // Destroy bullet
    // ==============
    public void destroy() {	
        BlockMap.entities.remove(this);
        fallback=null;
    }

    // ====================
    // Shoot collision code
    // ====================
    public boolean isCollisionWith(LevelObject other) {
        
        if (other.type.equals("marine")){
            if (poly.intersects(other.poly)){
                other.gotHurt=true;
            }
        }
        if (other.type.equals("samus")){
            if (poly.intersects(other.poly)){
                System.out.println("samus got hurt");
                other.gotHurt=true;
            }
        }
        if (other.type.equals("bullet"))
            return false;

        if (other instanceof Sas)
            return false;

        return poly.intersects(other.poly);
    }

    public boolean collidedWith(LevelObject entity) {
        return true;
    }


    public void draw(Graphics g) {
        if (fallback!=null)
            g.drawAnimation(fallback, x, y); // draw bullet sprite
        if (debugMode)
            g.draw(poly);                  // draw bullet bounding box
    }
}
